package com.sansecy.app;

import android.Manifest;
import android.app.Activity;
import android.content.pm.PackageManager;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.ActivityCompat;
import android.util.Log;
import android.view.Gravity;
import android.widget.FrameLayout;
import android.widget.Toast;

import com.taobao.android.mnndemo.CameraView;
import com.taobao.android.mnndemo.ImageActivity;

public class CameraActivity extends Activity {
    private static final String TAG = "CameraActivity";
    private CameraView mCameraView;
    private FrameLayout frameLayout;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        frameLayout = new FrameLayout(this);
        frameLayout.setBackgroundColor(Color.BLACK);
        setContentView(frameLayout);

        Toast.makeText(this, "setContentView", Toast.LENGTH_SHORT).show();
        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CAMERA}, 1);
            Toast.makeText(this, "requestPermissions", Toast.LENGTH_SHORT).show();
            Log.d(TAG, "onCreate() called with: requestPermissions = [" + savedInstanceState + "]");
        } else {
            Log.d(TAG, "onCreate() called with: has Permissions = [" + savedInstanceState + "]");
            Toast.makeText(this, "has Permissions", Toast.LENGTH_SHORT).show();
            createCameraView(frameLayout);
        }
    }

    private void createCameraView(FrameLayout frameLayout) {
        mCameraView = new CameraView(this);
        FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(-1, -1);
        layoutParams.gravity = Gravity.CENTER;
        frameLayout.addView(mCameraView, layoutParams);
        if (mCameraView != null) {
            mCameraView.setPreviewCallback(new CameraView.PreviewCallback() {
                @Override
                public void onGetPreviewOptimalSize(int optimalWidth, int optimalHeight) {

                }

                @Override
                public void onPreviewFrame(byte[] data, int imageWidth, int imageHeight, int angle) {

                }
            });
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (grantResults.length > 0) {
            createCameraView(frameLayout);
            if (mCameraView != null) {
                mCameraView.onResume();
            }
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        new Handler().post(new Runnable() {
            @Override
            public void run() {
                if (mCameraView != null) {
                    mCameraView.onResume();
                }
            }
        });
    }

    @Override
    protected void onPause() {
        super.onPause();
        if (mCameraView != null) {
            mCameraView.onPause();
        }
    }
}
